home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / TINY_WP.ZIP / SCRIPT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-02  |  6.0 KB  |  244 lines

  1. /* ---------- script.c -------------- */
  2.  
  3. /*
  4.  * The SI shell to implement interpreted scripts in SMALLCOM
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <conio.h>
  9. #include <stdlib.h>
  10. #include <ctype.h>
  11. #include <dos.h>
  12. #include <setjmp.h>
  13. #include "window.h"
  14. #include "serial.h"
  15. #include "interp.h"
  16. #include "modem.h"
  17.  
  18. #if COMPILER == MSOFT
  19. #define MK_FP(s,o) ((void far *) \
  20.     (((unsigned long)(s) << 16) | (unsigned)(o)))
  21. #endif
  22.  
  23. void upload_ASCII(FILE *);
  24. void download_ASCII(FILE *);
  25. void upload_xmodem(FILE *);
  26. void download_xmodem(FILE *);
  27. int waitforstring(char **, int, int);
  28. char *prompt_line(char *, int, char *);
  29. void reset_prompt(char *, int);
  30.  
  31. /* ----------- intrinsic interpreter functions ---------- */
  32. static int si_logon(int *);
  33. static int si_logoff(int *);
  34. static int si_upload(int *);
  35. static int si_download(int *);
  36. static int si_hangup(int *);
  37. static int si_quit(int *);
  38. static int si_sendstring(int *);
  39. static int si_sendchar(int *);
  40. static int si_waitforstrings(int *);
  41. static int si_waitfor(int *);
  42. static int si_system(int *);
  43. static int si_message(int *);
  44.  
  45. INTRINSIC ffs[] = {
  46.     "logon",            si_logon,
  47.     "logoff",           si_logoff,
  48.     "upload",           si_upload,
  49.     "download",         si_download,
  50.     "hangup",           si_hangup,
  51.     "quit",             si_quit,
  52.     "sendstring",       si_sendstring,
  53.     "sendchar",         si_sendchar,
  54.     "waitforstrings",   si_waitforstrings,
  55.     "waitfor",          si_waitfor,
  56.     "system",           si_system,
  57.     "message",          si_message,
  58.     NULL,               NULL
  59. };
  60.  
  61. extern INTRINSIC *infs = ffs;
  62. extern FILE *logfp;
  63.  
  64. /* ------------------ error messages ----------------------- */
  65. char *erm[]={  "Unexpected end of file", "Unrecognized",
  66.                "Duplicate ident",        "Symbol table full",
  67.                "Out of heap memory",     "Undeclared ident",
  68.                "Syntax Error",           "Unmatched {}",
  69.                "Unmatched ()",           "Missing",
  70.                "Not a function",         "Misplaced break",
  71.                "Out of place",           "Too many strings",
  72.                "Token buffer overflow",  "Divide by zero"    };
  73.  
  74. static FILE *fp;
  75. static char *prompt = NULL;
  76. extern char scriptfile[];
  77. extern char *tokenbf;
  78.  
  79. jmp_buf errorjmp;
  80.  
  81. /* ---------- process the named script file --------- */
  82. void script()
  83. {
  84.     if ((fp = fopen(scriptfile, "r")) != NULL) {
  85.         if (setjmp(errorjmp) == 0)  {
  86.             loadsource();
  87.             interpret();
  88.         }
  89.         fclose(fp);
  90.         if (prompt != NULL)
  91.             reset_prompt(prompt, 25);
  92.         if (tokenbf != NULL)
  93.             free(tokenbf);
  94.     }
  95. }
  96.  
  97. /* ----------- syntax error in script language --------- */
  98. void sierror(enum errs erno, char *s, int line)
  99. {
  100.     char msg[80];
  101.     sprintf(msg, "SI Error: %s %s line %d\n",s,erm[erno],line);
  102.     error_message(msg);
  103.     longjmp(errorjmp, 1);
  104. }
  105.  
  106. /* ---------- get a character of script source code -------- */
  107. int getsource(void)
  108. {
  109.     return getc(fp);
  110. }
  111.  
  112. /* -------- unget a character of script source code ------- */
  113. void ungetsource(int c)
  114. {
  115.     ungetc(c, fp);
  116. }
  117.  
  118. /* ------------ intrinsic functions -------------- */
  119.  
  120. /* ---------- turn logging on ----------------- */
  121. static int si_logon(int *ptr)
  122. {
  123.     si_logoff(ptr);
  124.     logfp = fopen((char *) *ptr, "ab");
  125.     return 0;
  126. }
  127.  
  128. /* ---------- turn logging off ----------------- */
  129. static int si_logoff(int *ptr)
  130. {
  131.     if (logfp)
  132.         fclose(logfp);
  133.     logfp = NULL;
  134.     return 0;
  135. }
  136.  
  137. /* ----------- upload a file ----------------- */
  138. static int si_upload(int *ptr)
  139. {
  140.     FILE *up;
  141.     int x = wherex();
  142.     int y = wherey();
  143.  
  144.     if ((up = fopen((char *) ptr[0], "rb")) != NULL)    {
  145.         if (toupper(ptr[1]) == 'A')
  146.             upload_ASCII(up);
  147.         else if (toupper(ptr[1]) == 'X')
  148.             upload_xmodem(up);
  149.         fclose(up);
  150.     }
  151.     gotoxy(x,y);
  152.     return 0;
  153. }
  154.  
  155. /* --------- download a file ------------- */
  156. static int si_download(int *ptr)
  157. {
  158.     FILE *dn;
  159.     int x = wherex();
  160.     int y = wherey();
  161.  
  162.     if ((dn = fopen((char *) ptr[0], "wb")) != NULL)    {
  163.         if (toupper(ptr[1]) == 'A')
  164.             download_ASCII(dn);
  165.         else if (toupper(ptr[1]) == 'X')
  166.             download_xmodem(dn);
  167.         fclose(dn);
  168.     }
  169.     gotoxy(x,y);
  170.     return 0;
  171. }
  172.  
  173. /* ---------- hangup the call ------------ */
  174. static int si_hangup(int *ptr)
  175. {
  176.     disconnect();
  177.     return 0;
  178. }
  179.  
  180. /* ----------- terminate the program ---------- */
  181. static int si_quit(int *ptr)
  182. {
  183.     int far *bp = MK_FP(0x40, 0x1a); /* BIOS read-ahead buff */
  184.  
  185.     if (*ptr)   {
  186.         *bp++ = 0x1e;   /* next off pointer */
  187.         *bp++ = 0x22;   /* next on pointer  */
  188.         *bp++ = 27;     /* Esc key          */
  189.         *bp   = 'y';    /* 'y' for Yes      */
  190.     }
  191.     longjmp(errorjmp, 1);
  192. }
  193.  
  194. /* ---------- send a string to the callee --------- */
  195. static int si_sendstring(int *ptr)
  196. {
  197.     char *cp = (char *) *ptr;
  198.  
  199.     while (*cp)
  200.         writecomm(*cp++);
  201.     return 0;
  202. }
  203.  
  204. /* ---------- send a character to the callee --------- */
  205. static int si_sendchar(int *ptr)
  206. {
  207.     writecomm(*ptr);
  208.     return 0;
  209. }
  210.  
  211. /* --- wait for one of a set of strings from the callee --- */
  212. static int si_waitforstrings(int *ptr)
  213. {
  214.     return waitforstring((char **) ptr[0], 60, 0);
  215. }
  216.  
  217. /* ------- wait for a string from the callee ------- */
  218. static int si_waitfor(int *ptr)
  219. {
  220.     static char *ws[] = {NULL, NULL};
  221.  
  222.     ws[0] = (char *) *ptr;
  223.     return waitforstring(ws, 60, 0);
  224. }
  225.  
  226. /* ---------- execute a system (DOS) command ----------- */
  227. static int si_system(int *ptr)
  228. {
  229.     char cmd[80];
  230.     sprintf(cmd, "%s >nul", (char *) *ptr);
  231.     system(cmd);
  232.     return 0;
  233. }
  234.  
  235. /* --------- display a message to the user ------------ */
  236. static int si_message(int *ptr)
  237. {
  238.     int x = wherex();
  239.     int y = wherey();
  240.     prompt = prompt_line((char *) *ptr, 25, prompt);
  241.     gotoxy(x,y);
  242.     return 0;
  243. }
  244.